<?php
namespace app\api\controller;
use app\BaseController;
use app\common\model\FileModel;

use Qiniu\Auth;
class ManageFile extends BaseController{
    //获取七牛token
    public function qiniuToken(){
        $ak = 'GjBg2wPY44oIq7ybIQM-bHP8gN7bgIm_dli5kidL';
        $sk = 'GBmz_EkCmPv9hhAzaDRpPl8cUcrAJ4FXUBXPwycz';
        $bucket = 'xhmmp';
        $auth = new Auth(trim($ak), trim($sk));
        $token = $auth->uploadToken(trim($bucket));
        return success($token);
    }
    //上传资源
    public function upload(){
        
    }
    //获取资源列表
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        $category_id = $this->request->param('category_id/d',0);//分类
        $file_type = $this->request->param('file_type/d',0);//类型 0-全部 1-图片 2-视频 3-文档

        $where=[
            // ['firm_id','=',$this->request->user_info->user_id],
            ['user_id','=',$this->request->user_info->user_id],
            ['is_delete','=',0]
        ];
        if(!empty($category_id)){
            $where[] = ['category_id','=',$category_id];
        }
        if(!empty($file_type)){
            $where[] = ['file_type','=',$file_type];
        }
        if(!empty($keyword)){
            $where[] = ['file_name','LIKE','%'.$keyword.'%'];
        }

        $list = FileModel::field('file_id,file_name,file_type,url')
            ->where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('file_id','desc')
            ->select();

        $total = FileModel::where($where)->count('file_id');
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    //新增/修改资源
    public function save(){
        $file_id = $this->request->param('file_id/d',0);
        
        $category_id = $this->request->param('category_id/d',0);//分类
       
        $file_name = $this->request->param('file_name/s','未命名');//资源名称

        
        if($file_id > 0){
            if(null === $fileModel = FileModel::find($file_id)){
                return error('资源不存在');
            }
            $fileModel->category_id = $category_id;
            $fileModel->file_name = $file_name;
            if($fileModel->save()){
                return  success('修改成功');
            }else{
                return error('修改失败');
            }
        }else{
            $form_type = $this->request->param('form_type/s','miniapp');//来源
            $file_type = $this->request->param('file_type/d',0);//类型 0-全部 1-图片 2-视频 3-文档
            $hash = $this->request->param('hash/s','');
            $key = $this->request->param('key/s','');
            $url = $this->request->param('url/s','');
    
            $size = $this->request->param('size/d',0);
            $ext = $this->request->param('ext/s','');
            
            $firm_id = $this->request->param('firmId/d',0);
            
            if($firm_id == 0){
                return error('参数错误#101');
            }
        
            $fileModel = new FileModel();
            $fileModel->category_id = $category_id;
            $fileModel->firm_id = $firm_id;
            $fileModel->form_type = $form_type;
            
            $fileModel->file_type = $file_type;
            $fileModel->file_name = $file_name;
            $fileModel->hash = $hash;
            $fileModel->key = $key;
            $fileModel->url = 'https://file.mp.xhm.net/'.$url;
            $fileModel->size = $size;
            $fileModel->ext = $ext;
            $fileModel->firm = $this->request->firm_id;
            $fileModel->user_id = $this->request->user_info->user_id;
            if($fileModel->save()){
                return  success(['url'=>$fileModel->url]);
            }else{
                return error('上传失败');
            }
        }
    }
    //删除资源
    public function delete(){
        $file_id = $this->request->param('file_id/d',0);
        if(empty($file_id)){
            return error('错误操作');
        }
        if(null === $fileModel = FileModel::find($file_id)){
            return error('文章不存在');
        }
        if($fileModel->firm_id != $this->request->firm_id){
            return error('没有权限');
        }
        $fileModel->is_delete = 1;
        $fileModel->save();
        return success('删除成功');
    }
}